package com.feihong.yw189.activity.kaika;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.base.BaseActivity;
import com.feihong.yw189.adapter.ExpandAbleListAdapter;
import com.feihong.yw189.app.App;
import com.feihong.yw189.listener.http.RequestCallBack;
import com.feihong.yw189.message.LoginMessage;
import com.feihong.yw189.model.Spec.SpecInfoModel;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 号码套餐   主页
 */

public class ChooseTaoCanActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_lin)
    ImageView ivLin;
    @BindView(R.id.txt_pagetitle)
    TextView txtPagetitle;

    @BindView(R.id.packageSpacList)
    ExpandableListView   mPackageLv;
    ExpandAbleListAdapter mAdapter;

    Intent mIntent;
    @BindView(R.id.connect_net_error_layout)
    LinearLayout mConnectNetErrorLayout;
    @BindView(R.id.requery_data_tv)
    TextView mRequeryDataTv;
    @BindView(R.id.lay_loading)
    RelativeLayout mLoadingLayout;
    @BindView(R.id.null_data_layout)
    RelativeLayout mNullDataLayout;
    @BindView(R.id.null_data_layout_txt)
    TextView mNullDataWarningTv;
    /**
     * 套餐类型  1  开卡套餐    2 宽带套餐
     */
    private String mSpecType;
    public static final String REQUEST_SPEC_TYPE = "REQUEST_SPEC_TYPE";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbertaocan);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    public void setTheme() {

    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    private void initView() {
        mSpecType = getIntent().getStringExtra(REQUEST_SPEC_TYPE);
        mNullDataLayout.setVisibility(View.GONE);
        mNullDataWarningTv.setText("暂无套餐信息");
        txtPagetitle.setText("号码套餐");
        ivLin.setVisibility(View.GONE);
        mAdapter = new ExpandAbleListAdapter(this);
        mAdapter.setInsertChildPackageDetailListener(new InsertChildPackageDetail());
        mPackageLv.setAdapter(mAdapter);

    }
    private void initData() {
        mPackageLv.setFocusable(false);
        queryData();
    }

    /**
     * 无数据
     */
    private void noDataInfo(){
        mConnectNetErrorLayout.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.GONE);
        mPackageLv.setVisibility(View.GONE);
        mNullDataLayout.setVisibility(View.VISIBLE);
    }

    /**
     * 连接服务器异常
     */
    private void connectNetError(){
        mLoadingLayout.setVisibility(View.GONE);
        mPackageLv.setVisibility(View.GONE);
        mNullDataLayout.setVisibility(View.GONE);
        mConnectNetErrorLayout.setVisibility(View.VISIBLE);
    }

    /**
     * 正常布局
     */
    private void normalLayout(){
        mConnectNetErrorLayout.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.GONE);
        mNullDataLayout.setVisibility(View.GONE);
        mPackageLv.setVisibility(View.VISIBLE);
    }

    private String KDDATA = "{\"status\":1,\"info\":\"套餐获取成功！\",\"data\":[{\"spec_g_id\":\"3345\",\"spec_g_name\":\"宽带4G测试组\",\"info\":\"测试专用组\",\"spec_type\":\"34\",\"spec_list\":[{\"spec_id\":\"18546\",\"spec_g_id\":\"3345\",\"spec_name\":\"单固话\",\"spec_info\":\"单固话\",\"price\":\"0.00\",\"dp_price\":\"0.00\",\"system_spec_id\":\"1804\",\"is_show\":\"1\",\"user_group_id\":\"8239\",\"show_parms\":{\"is_dizhi\":1,\"is_tel\":1,\"is_phone\":{\"min\":1,\"max\":2},\"is_input\":[{\"name\":\"主号码\",\"value\":\"mobile\",\"type\":\"number\"},{\"name\":\"机主姓名\",\"value\":\"name\",\"type\":\"text\"}],\"is_sulv\":[\"100\",\"200\",\"500\"],\"is_hongbao\":[]},\"region_id\":\"457\"},{\"spec_id\":\"17597\",\"spec_g_id\":\"3345\",\"spec_name\":\"宽带测试\",\"spec_info\":\"宽带测试\",\"price\":\"0.00\",\"dp_price\":\"0.00\",\"system_spec_id\":\"1652\",\"is_show\":\"1\",\"user_group_id\":\"8239\",\"show_parms\":{\"is_dizhi\":1,\"is_tel\":0,\"is_phone\":{\"min\":1,\"max\":2},\"is_input\":[{\"name\":\"主号码\",\"value\":\"mobile\"},{\"name\":\"机主姓名\",\"value\":\"name\"}],\"is_sulv\":[],\"is_hongbao\":[]},\"region_id\":\"457\"}],\"spec_type_name\":\"电信4G宽带套餐\"}]}";
    private String KKDATA = "{\"status\":1,\"info\":\"套餐获取成功！\",\"data\":[{\"spec_g_id\":\"3327\",\"spec_g_name\":\"开卡4G测试组\",\"info\":\"测试专用组\",\"spec_type\":\"30\",\"spec_list\":[{\"spec_id\":\"17544\",\"spec_g_id\":\"3327\",\"spec_name\":\"4G副卡新装套餐\",\"spec_info\":\"4G副卡新装套餐\",\"price\":\"0.00\",\"dp_price\":\"0.00\",\"system_spec_id\":\"1698\",\"is_show\":\"1\",\"user_group_id\":\"8239\",\"show_parms\":{\"is_phone\":{\"min\":1,\"max\":2},\"is_input\":[{\"name\":\"主号码\",\"value\":\"mobile\",\"type\":\"number\"},{\"name\":\"机主姓名\",\"value\":\"name\",\"type\":\"text\"}]},\"region_id\":\"455\"},{\"spec_id\":\"17573\",\"spec_g_id\":\"3327\",\"spec_name\":\"v团主卡\",\"spec_info\":\"v团主卡\",\"price\":\"0.00\",\"dp_price\":\"0.00\",\"system_spec_id\":\"1619\",\"is_show\":\"1\",\"user_group_id\":\"8239\",\"show_parms\":{\"is_phone\":{\"min\":1,\"max\":2},\"is_input\":[{\"name\":\"主号码\",\"value\":\"mobile\"},{\"name\":\"机主姓名\",\"value\":\"name\"}]},\"region_id\":\"455\"}],\"spec_type_name\":\"电信4G套餐\"},{\"spec_g_id\":\"3326\",\"spec_g_name\":\"开卡3G测试组\",\"info\":\"测试专用组\",\"spec_type\":\"3\",\"spec_list\":[{\"spec_id\":\"17543\",\"spec_g_id\":\"3326\",\"spec_name\":\"3G副卡新装套餐\",\"spec_info\":\"3G副卡新装套餐\",\"price\":\"0.00\",\"dp_price\":\"0.00\",\"system_spec_id\":\"1697\",\"is_show\":\"1\",\"user_group_id\":\"8239\",\"show_parms\":{\"is_dizhi\":1,\"is_tel\":1,\"is_phone\":{\"min\":1,\"max\":2},\"is_input\":[{\"name\":\"主号码\",\"value\":\"mobile\"},{\"name\":\"机主姓名\",\"value\":\"name\"}],\"is_sulv\":[\"100\",\"200\",\"500\"],\"is_hongbao\":[]},\"region_id\":\"455\"}],\"spec_type_name\":\"电信3G套餐\"},{\"spec_g_id\":\"2969\",\"spec_g_name\":\"电销4G--不限量套餐组\",\"info\":\"电销4G--不限量套餐组\",\"spec_type\":\"30\",\"spec_list\":[{\"spec_id\":\"14385\",\"spec_g_id\":\"2969\",\"spec_name\":\" 天翼畅享40GB（299元）套餐201802\",\"spec_info\":\" 天翼畅享40GB（299元）套餐201802\",\"price\":\"650.00\",\"dp_price\":\"650.00\",\"system_spec_id\":\"1709\",\"is_show\":\"1\",\"user_group_id\":\"3145\",\"show_parms\":{\"is_dizhi\":1,\"is_tel\":1,\"is_phone\":{\"min\":1,\"max\":2},\"is_input\":[{\"name\":\"主号码\",\"value\":\"mobile\"},{\"name\":\"机主姓名\",\"value\":\"name\"}],\"is_sulv\":[\"100\",\"200\",\"500\"],\"is_hongbao\":[]},\"region_id\":\"457\"}],\"spec_type_name\":\"电信4G套餐\"}]}";
    private void queryData() {
        mLoadingLayout.setVisibility(View.VISIBLE);
//        SpecInfoModel kaTcModel= JSON.parseObject(KKDATA,SpecInfoModel.class);
//        if(kaTcModel != null){
//            if(kaTcModel.getStatus() == 1){
//                List<SpecInfoModel.DataBean> spaceGroups = kaTcModel.getData();
//                if(spaceGroups != null && spaceGroups.size() >0){
//                    if(mPackageLv.getVisibility() == View.GONE){
//                        normalLayout();
//                    }
//                    mPackageLv.setVisibility(View.VISIBLE);
//                    mAdapter.setData(spaceGroups);
//                }else{
//                    noDataInfo();
//                }
//            }else{
//                noDataInfo();
//                showToast(""+kaTcModel.getInfo());
//            }
//        }else{
//            noDataInfo();
//        }
        App.getApp().getHttpTools().getSpecInfo(mSpecType,new RequestCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                if(activityIsDestory()){return;}
                SpecInfoModel kaTcModel= JSON.parseObject(result,SpecInfoModel.class);
                    if(kaTcModel != null){
                        if(kaTcModel.getStatus() == 1){
                            List<SpecInfoModel.DataBean> spaceGroups = kaTcModel.getData();
                            if(spaceGroups != null && spaceGroups.size() >0){
                                if(mPackageLv.getVisibility() == View.GONE){
                                    normalLayout();
                                }
                                mPackageLv.setVisibility(View.VISIBLE);
                                mAdapter.setData(spaceGroups);
                            }else{
                                noDataInfo();
                            }
                        }else{
                            noDataInfo();
                            showToast(""+kaTcModel.getInfo());
                        }
                    }else{
                        noDataInfo();
                    }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(activityIsDestory()){return;}
                connectNetError();
            }
            @Override
            public void onCancelled(Callback.CancelledException cex) {
            }
            @Override
            public void onFinished() {
                if(activityIsDestory()){return;}
                mLoadingLayout.setVisibility(View.GONE);
            }
        });
    }

    @OnClick({R.id.iv_back,R.id.requery_data_tv
    })
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();//返回
                break;
            case R.id.requery_data_tv:
                normalLayout();
                queryData();
                break;
            default:break;
        }
    }

    public static final String KAIKA_SELECT_PACKAGE_KEY = "KAIKA_SELECT_PACKAGE_KEY";
    public static final String KAIKA_SELECT_PACAKGE_GROUPID_KEY = "KAIKA_SELECT_PACAKGE_GROUPID_KEY";
    public static final String KAIKA_SELECT_PACAKGE_ISDOUBLE_KEY = "KAIKA_SELECT_PACAKGE_ISDOUBLE_KEY";
    public static final String KAIKA_SELECT_PACAKGE_ISPHONE_KEY = "KAIKA_SELECT_PACAKGE_ISPHONE_KEY";
    public static final String KAIKA_FUKA = "KAIKA_FUKA";

    class InsertChildPackageDetail implements ExpandAbleListAdapter.InsertChildPackageDetailListener{

        @Override
        public void insertChildPackageDetailListener(SpecInfoModel.DataBean.SpecListBean childData) {

            mIntent = new Intent(ChooseTaoCanActivity.this,KaiKaActivity.class);
            Bundle bundle = new Bundle();
            bundle.putParcelable(KAIKA_SELECT_PACKAGE_KEY,childData);
            mIntent.putExtras(bundle);
//            mIntent.putExtra(KAIKA_SELECT_PACAKGE_GROUPID_KEY,childData.getSpec_g_id());
//            mIntent.putExtra(KAIKA_SELECT_PACAKGE_ISDOUBLE_KEY,childData.getSystem_spec_info().getIs_double());
//            mIntent.putExtra(KAIKA_SELECT_PACAKGE_ISPHONE_KEY,childData.getSystem_spec_info().getShow_parms().getIs_phone());
//                mIntent.putExtra(KAIKA_FUKA,childData.getSystem_spec_info().getShow_parms().getFuka_xinzhuang());
//            if(!TextUtils.isEmpty(childData.getSystem_spec_info().getShow_parms().getFuka_xinzhuang())){
            // EventBus.getDefault().postSticky(childData.getSystem_spec_info().getShow_parms().getFuka_xinzhuang());
//            }
            setResult(RESULT_OK,mIntent);
            finish();
        }

        @Override
        public void insertChildImageGoDetailListener(SpecInfoModel.DataBean.SpecListBean childData) {

            mIntent = new Intent(ChooseTaoCanActivity.this,TaoCanDetailActivity.class);
            mIntent.putExtra("system_spec_id",childData.getSystem_spec_id());
            startActivity(mIntent);
        }
    }
}
